﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using System.Xml;
using System.Xml.Linq;

namespace CodeGenerator.Control
{
    class CGenerateClass
    {
        /// <summary>
        /// 生成所有的类，
        /// 包括Model下的Entity和hbm.mxl,
        /// DLL下的DAO和DAOHibernate,
        /// BLL下的IService和Service，
        /// Views下的View和ViewImpl
        /// Presentions下的Presenter和PresenterImpl
        /// </summary>
        /// <param name="dt">字段集合</param>
        /// <param name="tableName">表名</param>
        /// <param name="className">类名</param>
        /// <param name="classNameSpace">命名空间</param>
        /// <param name="fileUrl">生成路径</param>
        /// <returns>实体类的代码内容字符</returns>
        public void BuildAllClass(string tableName, DataTable dt, string className, string classNameSpace, string fileUrl)
        {
            //生成MODEL部分
            BuildClass(tableName, dt, className, classNameSpace, fileUrl, CGenerateClassTypeEnum.MODELENTITY);
            BuildClass(tableName, dt, className, classNameSpace, fileUrl, CGenerateClassTypeEnum.MODELHBM);

            //生成DLL部分
            BuildClass(tableName, dt, className, classNameSpace, fileUrl, 
CGenerateClassTypeEnum.DLLDAO);
            BuildClass(tableName, dt, className, classNameSpace, fileUrl, CGenerateClassTypeEnum.DLLDAOHIBERNATE);

            //生成BLL部分
            BuildClass(tableName, dt, className, classNameSpace, fileUrl, CGenerateClassTypeEnum.BLLINTERFACE);
            BuildClass(tableName, dt, className, classNameSpace, fileUrl, CGenerateClassTypeEnum.BLLCLASS);

            //生成VIEWS部分
            BuildClass(tableName, dt, className, classNameSpace, fileUrl, CGenerateClassTypeEnum.VIEWSINTERFACE);
            BuildClass(tableName, dt, className, classNameSpace, fileUrl, CGenerateClassTypeEnum.VIEWSCLASS);

            //生成PRESENTIONS部分
            BuildClass(tableName, dt, className, classNameSpace, fileUrl, CGenerateClassTypeEnum.PRESENTIONSINTERFACE);
            BuildClass(tableName, dt, className, classNameSpace, fileUrl, CGenerateClassTypeEnum.PRESENTIONSCLASS);

        }

        private void BuildClass(string tableName, DataTable dt, string className, string classNameSpace, string fileUrl, CGenerateClassTypeEnum classType)
        {
            string str = "";
            switch (classType)
            {
                case CGenerateClassTypeEnum.MODELENTITY:
                    str = GenerModelEntity(tableName, dt, className, classNameSpace, fileUrl);
                    break;
                case CGenerateClassTypeEnum.MODELHBM:
                    str = GenerModelHBM(tableName, dt, className, classNameSpace, fileUrl);
                    break;
                case CGenerateClassTypeEnum.DLLDAO:
                    str = GenerDLLDAO(tableName, dt, className, classNameSpace, fileUrl);
                    break;
                case CGenerateClassTypeEnum.DLLDAOHIBERNATE:
                    str = GenerDLLHibernate(tableName, dt, className, classNameSpace, fileUrl);
                    break;
                case CGenerateClassTypeEnum.BLLINTERFACE:
                    str = GenerBLLInterface(tableName, dt, className, classNameSpace, fileUrl);
                    break;
                case CGenerateClassTypeEnum.BLLCLASS:
                    str = GenerBLLClass(tableName, dt, className, classNameSpace, fileUrl);
                    break;
                case CGenerateClassTypeEnum.VIEWSINTERFACE:
                    str = GenerViewsInterface(tableName, dt, className, classNameSpace, fileUrl);
                    break;
                case CGenerateClassTypeEnum.VIEWSCLASS:
                    break;
                case CGenerateClassTypeEnum.PRESENTIONSINTERFACE:
                    str = GenerPresentionsInterface(tableName, dt, className, classNameSpace, fileUrl);
                    break;
                case CGenerateClassTypeEnum.PRESENTIONSCLASS:
                    str = GenerPresentionsClass(tableName, dt, className, classNameSpace, fileUrl);
                    break;
                default:
                    break;
            }
            WriteToFile(str, fileUrl, className, classType);
        }

        /// <summary>
        /// 生成Presenter的实现类
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="dt"></param>
        /// <param name="className"></param>
        /// <param name="classNameSpace"></param>
        /// <param name="fileUrl"></param>
        /// <returns></returns>
        private string GenerPresentionsClass(string tableName, DataTable dt, string className, string classNameSpace, string fileUrl)
        {
            StringBuilder strBuilder = new StringBuilder();

            //Using
            strBuilder.AppendLine("using System;");
            strBuilder.AppendLine("using System.Data;");
            strBuilder.AppendLine();

            //namespace
            strBuilder.AppendLine("namespace " + classNameSpace + ".Presentions.Presenter");
            strBuilder.AppendLine("{");

            //class desc
            strBuilder.AppendLine("    /// <summary>");
            strBuilder.AppendLine("    /// 表[" + tableName + "]的Presenter实现类");
            strBuilder.AppendLine("    /// </summary>");

            //class
            strBuilder.AppendLine("    public class " + className + "Presenter : I" + className + "Presenter");
            strBuilder.AppendLine("    {");

            //构造函数
            strBuilder.AppendLine("        private " + className + "Presenter()");
            strBuilder.AppendLine("        {");
            strBuilder.AppendLine("            throw new NotSupportedException(\"无接口的实现.\");");
            strBuilder.AppendLine("        }");
            strBuilder.AppendLine();

            strBuilder.AppendLine("        private " + className + "Presenter(I" + className + "Form view, I" + className + "Service " + className.Substring(className.IndexOf("_")) + "Service)");
            strBuilder.AppendLine("        {");
            strBuilder.AppendLine("            this.view = view;");
            strBuilder.AppendLine("            this." + className.Substring(className.IndexOf("_")) + "Service = " + className.Substring(className.IndexOf("_")) + "Service;");
            strBuilder.AppendLine("        }");
            strBuilder.AppendLine();

            //属性
            strBuilder.AppendLine("        #region 属性");
            strBuilder.AppendLine("        private I" + className.Substring(className.IndexOf("_")) + "Form view;");
            strBuilder.AppendLine("        public I" + className.Substring(className.IndexOf("_")) + "Form View");
            strBuilder.AppendLine("        {");
            strBuilder.AppendLine("            get{ return view;}");
            strBuilder.AppendLine("        }");
            strBuilder.AppendLine();

            strBuilder.AppendLine("        private I" + className.Substring(className.IndexOf("_")) + "Service service;");
            strBuilder.AppendLine("        public I" + className.Substring(className.IndexOf("_")) + "Service Service");
            strBuilder.AppendLine("        {");
            strBuilder.AppendLine("            get { return service; } ");
            strBuilder.AppendLine("        }");
            strBuilder.AppendLine();

            strBuilder.AppendLine("        #endregion 属性");

            strBuilder.AppendLine("    }");
            strBuilder.AppendLine("}");
            strBuilder.AppendLine();

            return strBuilder.ToString();
        }

        /// <summary>
        /// 生成Presenter的接口
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="dt"></param>
        /// <param name="className"></param>
        /// <param name="classNameSpace"></param>
        /// <param name="fileUrl"></param>
        /// <returns></returns>
        private string GenerPresentionsInterface(string tableName, DataTable dt, string className, string classNameSpace, string fileUrl)
        {
            StringBuilder strBuilder = new StringBuilder();

            //Using
            strBuilder.AppendLine("using System;");
            strBuilder.AppendLine("using System.Data;");
            strBuilder.AppendLine();

            //namespace
            strBuilder.AppendLine("namespace " + classNameSpace + ".Presentions.Presenter");
            strBuilder.AppendLine("{");

            //class desc
            strBuilder.AppendLine("    /// <summary>");
            strBuilder.AppendLine("    /// 表[" + tableName + "]的Presenter接口");
            strBuilder.AppendLine("    /// </summary>");

            //class
            strBuilder.AppendLine("    public interface I" + className + "Presenter");
            strBuilder.AppendLine("    {");

            //方法

            strBuilder.AppendLine("    }");
            strBuilder.AppendLine("}");

            return strBuilder.ToString();
        }

        private string GenerViewsInterface(string tableName, DataTable dt, string className, string classNameSpace, string fileUrl)
        {
            StringBuilder strBuilder = new StringBuilder();

            //Using
            strBuilder.AppendLine("using System;");
            strBuilder.AppendLine("using System.Data;");
            strBuilder.AppendLine();

            //namespace
            strBuilder.AppendLine("namespace " + classNameSpace + ".Views.View");
            strBuilder.AppendLine("{");

            //class desc
            strBuilder.AppendLine("    /// <summary>");
            strBuilder.AppendLine("    /// 表[" + tableName + "]的View接口");
            strBuilder.AppendLine("    /// </summary>");

            //class
            strBuilder.AppendLine("    public interface I" + className + "Form");
            strBuilder.AppendLine("    {");

            //字段属性：类型-名称
            strBuilder.AppendLine("        #region 表字段属性");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                strBuilder.AppendLine("        " + ConvertSystypesToCSharpType(dt.Rows[i][3].ToString()) + " " + dt.Rows[i][1].ToString() + " { get; set; }");
                strBuilder.AppendLine();
            }
            strBuilder.AppendLine("        #endregion");
            strBuilder.AppendLine();

            strBuilder.AppendLine("    }");
            strBuilder.AppendLine("}");

            return strBuilder.ToString();
        }

        /// <summary>
        /// 生成BLL下Service的实现类
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="dt"></param>
        /// <param name="className"></param>
        /// <param name="classNameSpace"></param>
        /// <param name="fileUrl"></param>
        /// <returns></returns>
        private string GenerBLLClass(string tableName, DataTable dt, string className, string classNameSpace, string fileUrl)
        {
            StringBuilder strBuilder = new StringBuilder();

            //Using
            strBuilder.AppendLine("using System;");
            strBuilder.AppendLine("using System.Data;");
            strBuilder.AppendLine();

            //namespace
            strBuilder.AppendLine("namespace " + classNameSpace + ".BLL.ServiceImpl");
            strBuilder.AppendLine("{");

            //class desc
            strBuilder.AppendLine("    /// <summary>");
            strBuilder.AppendLine("    /// 表[" + tableName + "]的Service实现类");
            strBuilder.AppendLine("    /// </summary>");

            //class
            strBuilder.AppendLine("    public class " + className + "Service : I" + className + "Service");
            strBuilder.AppendLine("    {");

            //property
            strBuilder.AppendLine("        private " + className + "DAO " + className.Substring(className.IndexOf("_")) + "DAO;");
            strBuilder.AppendLine("        public " + className + "DAO " + className + "DAO");
            strBuilder.AppendLine("        {");
            strBuilder.AppendLine("            get { return " + className.Substring(className.IndexOf("_")) + "DAO; }");
            strBuilder.AppendLine("            set { " + className.Substring(className.IndexOf("_")) + "DAO = value; }");
            strBuilder.AppendLine("        }");

            //method
            strBuilder.AppendLine("        public " + className + " Get(int id)");
            strBuilder.AppendLine("        {");
            strBuilder.AppendLine("            return " + className.Substring(className.IndexOf("_")) + "DAO.Get(id);");
            strBuilder.AppendLine("        }");

            strBuilder.AppendLine("        public IList<" + className + "> GetAll()");
            strBuilder.AppendLine("        {");
            strBuilder.AppendLine("            return " + className.Substring(className.IndexOf("_")) + "DAO.GetAll();");
            strBuilder.AppendLine("        }");

            strBuilder.AppendLine("        public int Save(" + className + " entity)");
            strBuilder.AppendLine("        {");
            strBuilder.AppendLine("            return " + className.Substring(className.IndexOf("_")) + "DAO.Save(entity);");
            strBuilder.AppendLine("        }");

            strBuilder.AppendLine("        public void Update(" + className + " entity)");
            strBuilder.AppendLine("        {");
            strBuilder.AppendLine("            " + className.Substring(className.IndexOf("_")) + "DAO.Update(entity);");
            strBuilder.AppendLine("        }");

            strBuilder.AppendLine("        public void Delete(" + className + " entity)");
            strBuilder.AppendLine("        {");
            strBuilder.AppendLine("            " + className.Substring(className.IndexOf("_")) + "DAO.Delete(entity);");
            strBuilder.AppendLine("        }");

            strBuilder.AppendLine("    }");
            strBuilder.AppendLine("}");

            return strBuilder.ToString();
        }

        /// <summary>
        /// 生成BLL的Service接口
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="dt"></param>
        /// <param name="className"></param>
        /// <param name="classNameSpace"></param>
        /// <param name="fileUrl"></param>
        /// <returns></returns>
        private string GenerBLLInterface(string tableName, DataTable dt, string className, string classNameSpace, string fileUrl)
        {
            StringBuilder strBuilder = new StringBuilder();

            //Using
            strBuilder.AppendLine("using System;");
            strBuilder.AppendLine("using System.Data;");
            strBuilder.AppendLine();

            //namespace
            strBuilder.AppendLine("namespace " + classNameSpace + ".BLL.Service");
            strBuilder.AppendLine("{");

            //class desc
            strBuilder.AppendLine("    /// <summary>");
            strBuilder.AppendLine("    /// 表[" + tableName + "]的Service接口");
            strBuilder.AppendLine("    /// </summary>");

            //class
            strBuilder.AppendLine("    public interface I" + className + "Service : IService<" + className + ", int>");
            strBuilder.AppendLine("    {");

            //method
            strBuilder.AppendLine("        " + className + " Get(int id);");
            strBuilder.AppendLine("        IList<" + className + "> GetAll();");
            strBuilder.AppendLine("        int Save(" + className + " " + className.Substring(className.IndexOf("_")) + ");");
            strBuilder.AppendLine("        void Update(" + className + " " + className.Substring(className.IndexOf("_")) + ");");
            strBuilder.AppendLine("        void Delete(" + className + " " + className.Substring(className.IndexOf("_")) + ");");

            strBuilder.AppendLine("    }");
            strBuilder.AppendLine("}");

            return strBuilder.ToString();
        }

        /// <summary>
        /// 生成DLL的DAOHibernate
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="dt"></param>
        /// <param name="className"></param>
        /// <param name="classNameSpace"></param>
        /// <param name="fileUrl"></param>
        /// <returns></returns>
        private string GenerDLLHibernate(string tableName, DataTable dt, string className, string classNameSpace, string fileUrl)
        {
            StringBuilder strBuilder = new StringBuilder();

            //Using
            strBuilder.AppendLine("using System;");
            strBuilder.AppendLine("using System.Data;");
            strBuilder.AppendLine();

            //namespace
            strBuilder.AppendLine("namespace " + classNameSpace + ".DAL.Hibernate");
            strBuilder.AppendLine("{");

            //class desc
            strBuilder.AppendLine("    /// <summary>");
            strBuilder.AppendLine("    /// 表[" + tableName + "]的Hibernate类实现");
            strBuilder.AppendLine("    /// </summary>");

            //class
            strBuilder.AppendLine("    public class " + className + "DAOHibernate : BaseHibernate<" + className + ", int>, " + className + "DAO");
            strBuilder.AppendLine("    {");
            strBuilder.AppendLine("    }");
            strBuilder.AppendLine("}");

            return strBuilder.ToString();
        }

        /// <summary>
        /// 生成DLL接口
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="dt"></param>
        /// <param name="className"></param>
        /// <param name="classNameSpace"></param>
        /// <param name="fileUrl"></param>
        /// <returns></returns>
        private string GenerDLLDAO(string tableName, DataTable dt, string className, string classNameSpace, string fileUrl)
        {
            StringBuilder strBuilder = new StringBuilder();

            //Using
            strBuilder.AppendLine("using System;");
            strBuilder.AppendLine("using System.Data;");
            strBuilder.AppendLine();

            //namespace
            strBuilder.AppendLine("namespace " + classNameSpace + ".DAL.DAO");
            strBuilder.AppendLine("{");

            //class desc
            strBuilder.AppendLine("    /// <summary>");
            strBuilder.AppendLine("    /// 表[" + tableName + "]的DAO接口");
            strBuilder.AppendLine("    /// </summary>");

            //interface
            strBuilder.AppendLine("    public interface " + className + "DAO : IDAO<" + className + ", int>");
            strBuilder.AppendLine("    {");
            strBuilder.AppendLine("    }");
            strBuilder.AppendLine("}");

            return strBuilder.ToString();
        }

        /// <summary>
        /// 生成model层下的Entity实体
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="dt"></param>
        /// <param name="className"></param>
        /// <param name="classNameSpace"></param>
        /// <param name="fileUrl"></param>
        /// <returns></returns>
        private string GenerModelEntity(string tableName, DataTable dt, string className, string classNameSpace, string fileUrl)
        {
            StringBuilder strBuilder = new StringBuilder();

            //Using
            strBuilder.AppendLine("using System;");
            strBuilder.AppendLine("using System.Data;");
            strBuilder.AppendLine();

            //namespace
            strBuilder.AppendLine("namespace " + classNameSpace);
            strBuilder.AppendLine("{");

            //class desc
            strBuilder.AppendLine("    /// <summary>");
            strBuilder.AppendLine("    /// 表[" + tableName + "]的实体类");
            strBuilder.AppendLine("    /// </summary>");
            
            //class
            strBuilder.AppendLine("    public class " + className);
            strBuilder.AppendLine("    {");

            //构造函数
            strBuilder.AppendLine("        public " + className + "(){}");

            //字段属性：类型-名称
            strBuilder.AppendLine("        #region 表字段属性");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                /**
                strBuilder.AppendLine("        private " + ConvertSystypesToCSharpType(dt.Rows[i][3].ToString()) + " _" + dt.Rows[i][1].ToString() + ";");
                strBuilder.AppendLine("        public " + ConvertSystypesToCSharpType(dt.Rows[i][3].ToString()) + " " + dt.Rows[i][1].ToString());
                strBuilder.AppendLine("        {");
                strBuilder.AppendLine("            get");
                strBuilder.AppendLine("            {");
                strBuilder.AppendLine("                return" + " _" + dt.Rows[i][1].ToString() + ";");
                strBuilder.AppendLine("            }");
                strBuilder.AppendLine("            set");
                strBuilder.AppendLine("            {");
                strBuilder.AppendLine("                " + "_" + dt.Rows[i][1].ToString() + " = value;");
                strBuilder.AppendLine("            }");
                strBuilder.AppendLine("        }");
                */

                strBuilder.AppendLine("        public virtual " + ConvertSystypesToCSharpType(dt.Rows[i][3].ToString()) + " " + dt.Rows[i][1].ToString() + "  { get; set; }");
                strBuilder.AppendLine();
            }
            strBuilder.AppendLine("        #endregion");
            strBuilder.AppendLine();

            strBuilder.AppendLine("    }");
            strBuilder.AppendLine("}");

            return strBuilder.ToString();
        }

        /// <summary>
        /// 生成xml文件
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="dt"></param>
        /// <param name="className"></param>
        /// <param name="classNameSpace"></param>
        /// <param name="fileUrl"></param>
        /// <returns></returns>
        private string GenerModelHBM(string tableName, DataTable dt, string className, string classNameSpace, string fileUrl)
        {

            XmlDocument xml = new XmlDocument();
            XmlDeclaration dec = xml.CreateXmlDeclaration("1.0", "UTF-8", null);
            xml.AppendChild(dec);

            //hibernate-mapping
            XmlNode rootnode = xml.CreateNode(XmlNodeType.Element, "hibernate-mapping", "");
            XmlAttribute a1 = xml.CreateAttribute("assembly");
            a1.InnerText = classNameSpace;
            XmlAttribute a2 = xml.CreateAttribute("namespace");
            a2.InnerText = classNameSpace;
            XmlAttribute a3 = xml.CreateAttribute("xmlns");
            a3.InnerText = "urn:nhibernate-mapping-2.2";
            rootnode.Attributes.Append(a1);
            rootnode.Attributes.Append(a2);
            rootnode.Attributes.Append(a3);

            //class
            XmlNode classNode = xml.CreateNode(XmlNodeType.Element, "class", "");
            XmlAttribute nameAttr = xml.CreateAttribute("name");
            nameAttr.InnerText = tableName;
            XmlAttribute tableAttr = xml.CreateAttribute("table");
            tableAttr.InnerText = tableName;
            XmlAttribute lazyAttr = xml.CreateAttribute("lazy");
            lazyAttr.InnerText = "true";
            classNode.Attributes.Append(nameAttr);
            classNode.Attributes.Append(tableAttr);
            classNode.Attributes.Append(lazyAttr);
            rootnode.AppendChild(classNode);

            //id或property
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (dt.Rows[i][5].ToString().Equals("1"))//id
                {
                    XmlNode idNode = xml.CreateNode(XmlNodeType.Element, "id", "");
                    XmlAttribute nameAttr2 = xml.CreateAttribute("name");
                    nameAttr2.InnerText = dt.Rows[i][1].ToString();
                    XmlAttribute columnAttr = xml.CreateAttribute("column");
                    columnAttr.InnerText = dt.Rows[i][1].ToString();
                    XmlAttribute typeAttr = xml.CreateAttribute("type");
                    typeAttr.InnerText = dt.Rows[i][3].ToString();
                    idNode.Attributes.Append(nameAttr2);
                    idNode.Attributes.Append(columnAttr);
                    idNode.Attributes.Append(typeAttr);
                    classNode.AppendChild(idNode);

                    XmlNode generatorNode = xml.CreateNode(XmlNodeType.Element, "generator", "");
                    XmlAttribute classAttr2 = xml.CreateAttribute("class");
                    classAttr2.InnerText = "native";
                    generatorNode.Attributes.Append(classAttr2);
                    idNode.AppendChild(generatorNode);
                }
                else
                {
                    XmlNode idNode = xml.CreateNode(XmlNodeType.Element, "property", "");
                    XmlAttribute nameAttr2 = xml.CreateAttribute("name");
                    nameAttr2.InnerText = dt.Rows[i][1].ToString();
                    XmlAttribute columnAttr = xml.CreateAttribute("column");
                    columnAttr.InnerText = dt.Rows[i][1].ToString();
                    XmlAttribute typeAttr = xml.CreateAttribute("type");
                    typeAttr.InnerText = dt.Rows[i][3].ToString();
                    idNode.Attributes.Append(nameAttr2);
                    idNode.Attributes.Append(columnAttr);
                    idNode.Attributes.Append(typeAttr);
                    classNode.AppendChild(idNode);
                }
            }

            xml.AppendChild(rootnode);
            xml.Save(fileUrl + tableName + ConvertClassFileType(className, CGenerateClassTypeEnum.MODELHBM));
            /*
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            settings.NewLineOnAttributes = true;

            XmlWriter writer = XmlWriter.Create(fileUrl + tableName + ConvertClassFileType(CGenerateClassTypeEnum.MODELHBM), settings);
            writer.WriteStartDocument();

            //设置xml属性
            //hibernate-mapping
            writer.WriteStartElement("hibernate-mapping");
            writer.WriteAttributeString("assembly", classNameSpace);
            writer.WriteAttributeString("namespace", classNameSpace);
            writer.WriteAttributeString("xmlns", "ns", null, "urn:nhibernate-mapping-2.2");

            //class
            writer.WriteStartElement("class");
            writer.WriteAttributeString("name", tableName);
            writer.WriteAttributeString("table", tableName);
            writer.WriteAttributeString("lazy", "true");

            //id或property
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (dt.Rows[i][5].ToString().Equals("1"))//id
                {
                    writer.WriteStartElement("id");
                    writer.WriteAttributeString("name", dt.Rows[i][1].ToString());
                    writer.WriteAttributeString("column", dt.Rows[i][1].ToString());
                    writer.WriteAttributeString("type", dt.Rows[i][3].ToString());

                    writer.WriteStartElement("generator");
                    writer.WriteAttributeString("class", "native");
                    writer.WriteEndElement();

                    writer.WriteEndElement();
                }
                else
                {
                    writer.WriteStartElement("property");
                    writer.WriteAttributeString("name", dt.Rows[i][1].ToString());
                    writer.WriteAttributeString("column", dt.Rows[i][1].ToString());
                    writer.WriteAttributeString("type", dt.Rows[i][3].ToString());
                    writer.WriteEndElement();
                }
            }

            writer.WriteEndElement();//class
            
            writer.WriteEndElement();//hibernate-mapping
   //         writer.WriteEndDocument();//xml
            //clear up
            writer.Flush();
            writer.Close();
            */
            return "";
        }



        /// <summary>
        /// 将生成的string写入到file中
        /// </summary>
        /// <param name="buildValue">生成string</param>
        /// <param name="fileUrl">文件url</param>
        /// <param name="className">类名称</param>
        /// <param name="classType">类类型</param>
        private void WriteToFile(string buildValue, string fileUrl, string className, CGenerateClassTypeEnum classType)
        {
            string fileUrlInfo = fileUrl + className + ConvertClassFileType(className, classType);
            FileInfo info = new FileInfo(@fileUrlInfo);
            if (!info.Directory.Exists)
            {
                Directory.CreateDirectory(info.DirectoryName);
            }
            StreamWriter stream = null;
            //保存
            try
            {
                //FileStream fs = new FileStream(@fileUrlInfo, FileMode.Create);
                stream = new StreamWriter(@fileUrlInfo, true);//, false, Encoding.UTF8, 1024
                stream.Write(buildValue);
                stream.Flush();
            }
            catch
            {
                throw;
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }
            }
        }

        /// <summary>
        /// 数据库数据类型转换为C#数据类型
        /// </summary>
        /// <param name="types">数据库数据类型</param>
        /// <returns>C#数据类型</returns>
        private string ConvertSystypesToCSharpType(string types)
        {
            string reval = string.Empty;
            switch (types.ToLower())
            {
                case "int":
                    reval = "Int32";
                    break;
                case "text":
                    reval = "string";
                    break;
                case "bigint":
                    reval = "Int64";
                    break;
                case "binary":
                    reval = "byte[]";
                    break;
                case "bit":
                    reval = "bool";
                    break;
                case "char":
                    reval = "string";
                    break;
                case "datetime":
                    reval = "DateTime";
                    break;
                case "decimal":
                    reval = "decimal";
                    break;
                case "float":
                    reval = "double";
                    break;
                case "image":
                    reval = "byte[]";
                    break;
                case "money":
                    reval = "decimal";
                    break;
                case "nchar":
                    reval = "string";
                    break;
                case "ntext":
                    reval = "string";
                    break;
                case "numeric":
                    reval = "decimal";
                    break;
                case "nvarchar":
                    reval = "string";
                    break;
                case "real":
                    reval = "Single";
                    break;
                case "smalldatetime":
                    reval = "DateTime";
                    break;
                case "smallint":
                    reval = "Int16";
                    break;
                case "smallmoney":
                    reval = "decimal";
                    break;
                case "timestamp":
                    reval = "DateTime";
                    break;
                case "tinyint":
                    reval = "byte";
                    break;
                case "uniqueidentifier":
                    reval = "guid";
                    break;
                case "varbinary":
                    reval = "byte[]";
                    break;
                case "varchar":
                    reval = "string";
                    break;
                case "variant":
                    reval = "object";
                    break;
                default:
                    reval = "string";
                    break;
            }
            return reval;
        }

        private string ConvertClassFileType(string className, CGenerateClassTypeEnum classtype) 
        {
            string type = string.Empty;
            switch (classtype)
            {
                case CGenerateClassTypeEnum.MODELHBM:
                    type = "/Model/" + className + ".hbm.xml";
                    break;
                case CGenerateClassTypeEnum.MODELENTITY:
                    type = "/Model/" + className + ".cs" ;
                    break;
                case CGenerateClassTypeEnum.DLLDAO:
                    type = "/DAL/DAO/" + className + "DAO.cs";
                    break;
                case CGenerateClassTypeEnum.DLLDAOHIBERNATE:
                    type = "/DAL/Hibernate/" + className + "DAOHibernate.cs";
                    break;
                case CGenerateClassTypeEnum.BLLINTERFACE:
                    type = "/BLL/Service/I" + className + "Service.cs";
                    break;
                case CGenerateClassTypeEnum.BLLCLASS:
                    type = "/BLL/ServiceImpl/" + className + "Service.cs";
                    break;
                case CGenerateClassTypeEnum.VIEWSINTERFACE:
                    type = "/Views/View/I" + className + "Form.cs";
                    break;
                case CGenerateClassTypeEnum.VIEWSCLASS:
                    type = "/Views/View/" + className + "Form.cs";
                    break;
                case CGenerateClassTypeEnum.PRESENTIONSINTERFACE:
                    type = "/Presentions/Presenter/I" + className + "Presenter.cs";
                    break;
                case CGenerateClassTypeEnum.PRESENTIONSCLASS:
                    type = "/Presentions/PresenterImpl/" + className + "Presenter.cs";
                    break;
                default:
                    type = ".cs";
                    break;
            }
            return type;
        }
        
    }
}
